﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RunCoachWebApplication.RunCoachServiceReference;

namespace RunCoachWebApplication.Controllers
{
    public class ActivityController : Controller
    {
        public ActivityController() { }

        RunCoachServiceClient _client = new RunCoachServiceClient();

        //
        // GET: /Activity/AllActivity
        public ActionResult AllActivity()
        {
            IEnumerable<Runs> _runs = _client.getAllRunsData((int)Session["UserId"]).ToList();
            return View(_runs);
        }

        //
        // GET: /Activity/AnActivity
        public ActionResult AnActivity(int runId)
        {
            List<GeoCoordinate> _coos = _client.getRunCoordinates(runId, (int)Session["UserId"]).ToList();
            List<Coordinate> coos = new List<Coordinate>();
            IEnumerable<Runs> _runs = _client.getAllRunsData((int)Session["UserId"]).ToList();
            Runs run = null;
            foreach (Runs item in _runs)
	        {
                if (item.id == runId) run = item;
	        }
            foreach (GeoCoordinate item in _coos)
            {
                String lat = item.Latitude.ToString().Replace(',', '.');
                String lon = item.Longitude.ToString().Replace(',', '.');
                coos.Add(new Coordinate(lat,lon));
            }
            ActivityParameter activityParameter = new ActivityParameter(coos, run);

            return View(activityParameter);
        }

        public class ActivityParameter
        {
            public List<Coordinate> coos;
            public Runs run;

            public ActivityParameter(List<Coordinate> coos, Runs run)
            {
                this.coos = coos;
                this.run = run;
            }
        };

        public class Coordinate
        {
            public String Lat;
            public String Lon;

            public Coordinate(String Lat, String Lon)
            {
                this.Lat = Lat;
                this.Lon = Lon;
            }
        };

        //
        // GET: /Activity/Delete
        public ActionResult Delete(int runId)
        {
            _client.deleteRunData(runId, (int)Session["UserId"]);
            return RedirectToAction("AllActivity", "Activity");
        }

    }
}
